﻿using System.Collections.Generic;
using System.Xml;
using SdmxMl.Common;

namespace SdmxMl.GenericData
{
    /// <summary>
    /// SeriesType specifies the structure of a series. This includes all of the key values, 
    /// values for all the attributes, and the set of observations making up the series content. 
    /// Messages may transmit only attributes, only data, or both. Regardless, the series key is 
    /// always required. Key values appear at the Series level in an ordered sequence which 
    /// corresponds to the key sequence in the key family. A series in a delete message need not 
    /// supply more than the key, indicating that the entire series identified by that key should 
    /// be deleted. If series attributes are sent in a delete message, any valid value specified 
    /// for an attribute indicates that the attribute should be deleted.
    /// </summary>
    public class SeriesType
    {
        #region Properties

        private ValueListTag _seriesKey;
        public ValueTypeList SeriesKey
        {
            get { return _seriesKey.ValueList; }
        }

        private ValueListTag _attributes;
        public ValueTypeList Attributes
        {
            get { return _attributes.ValueList; }
        }

        private ObsTypeList _obsList;
        public ObsTypeList ObsList
        {
            get { return _obsList; }
        }

        private AnnotationList annotationList;
        public AnnotationList AnnotationList
        {
            get { return annotationList; }
            set { annotationList = value; }
        }

        #endregion //Properties

        #region Construction

        private void Init()
        {
            AnnotationList = new AnnotationList();
            _obsList = new ObsTypeList();
        }

        public SeriesType()
        {
            Init();
            _seriesKey = new ValueListTag("SeriesKey");
            _attributes = new ValueListTag("Attributes");
        }

        public SeriesType(XmlNode nodeArtefact, XmlNamespaceManager nsmgr)
        {
            Init();

            _seriesKey = new ValueListTag("SeriesKey", nodeArtefact, nsmgr);
            _attributes = new ValueListTag("Attributes", nodeArtefact, nsmgr);

            XmlNodeList nodeList = nodeArtefact.SelectNodes("./generic:Obs", nsmgr);
            foreach (XmlNode nm in nodeList)
            {
                ObsType obs = new ObsType(nm, nsmgr);
                _obsList.Add(obs);
            }


            // Annotations
            nodeList = nodeArtefact.SelectNodes("./generic:Annotations/common:Annotation", nsmgr);
            foreach (XmlNode na in nodeList)
            {
                Annotation a = new Annotation(na, nsmgr);
                annotationList.Add(a);
            }
        }

        #endregion //Construction

        #region XML serialize
        public void WriteXml(XmlTextWriter xw, string prefix, string tag)
        {
            xw.WriteStartElement(prefix, tag, null);

            _seriesKey.WriteXml(xw);
            _attributes.WriteXml(xw);

            foreach (ObsType obs in _obsList)
                obs.WriteXml(xw, prefix, "Obs");

            // Annotations
            AnnotationList.WriteXml(xw, prefix, null);

            xw.WriteEndElement();
        }


        #endregion //XML serialize

    }


    #region List Container for SerieType

    public class SeriesTypeList : List<SeriesType>
    {
    }
    #endregion
}
